# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp import tools
from openerp.osv import fields, osv

class order_payable_report(osv.osv):
    _name = "order.payable.report"
    _description = u"订单回款时间统计"
    _auto = False
    _rec_name = 'date_confirm'

    _columns = {
        'amount_total': fields.float(u'订单金额', readonly=True),
        'margin': fields.float(u'毛利', readonly=True),
        'freight': fields.float(u'预收运费', readonly=True),
        'date_confirm': fields.date('Date Confirm', readonly=True),
        'partner_id': fields.many2one('res.partner', 'Partner', readonly=True),
        'company_id': fields.many2one('res.company', 'Company', readonly=True),
        'user_id': fields.many2one('res.users', 'Salesperson', readonly=True),
        'month_count': fields.integer(u'回款月份', readonly=True),
        'state': fields.selection([
            ('draft', 'Quotation'),
            ('waiting_date', 'Waiting Schedule'),
            ('manual', 'Manual In Progress'),
            ('progress', 'In Progress'),
            ('invoice_except', 'Invoice Exception'),
            ('done', 'Done'),
            ('cancel', 'Cancelled')
            ], 'Order Status', readonly=True),
        'is_enough_payment': fields.boolean(u'足够支付', readonly=True),
        'department_id': fields.many2one('hr.department', u'部门'),
    }
    _order = 'date_confirm desc'

    def _select(self):
        select_str = """
             SELECT min(s.id) as id,
                    sum(s.amount_total) as amount_total,
                    sum(s.margin) as margin,
                    sum(s.freight) as freight,
                    s.date_confirm as date_confirm,
                    s.partner_id as partner_id,
                    s.company_id as company_id,
                    s.user_id,
                    extract(month from age(s.enough_payment_date, s.date_confirm))+1 as month_count,
                    s.state,
                    s.is_enough_payment,
                    s.department_id as department_id
        """
        return select_str

    def _from(self):
        from_str = """
                sale_order s
        """
        return from_str

    def _group_by(self):
        group_by_str = """
            GROUP BY s.enough_payment_date,
                    s.date_confirm,
                    s.partner_id,
                    s.user_id,
                    s.company_id,
                    s.state,
                    s.is_enough_payment,
                    s.department_id
        """
        return group_by_str

    def init(self, cr):
        # self._table = sale_report
        tools.drop_view_if_exists(cr, self._table)
        cr.execute("""CREATE or REPLACE VIEW %s as (
            %s
            FROM %s
            %s
            )""" % (self._table, self._select(), self._from(), self._group_by()))

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
